"""
global和nonlocal的用法
"""
from utils.test_tools import test_decorator

# 次数
time = 1


@test_decorator('global')
def speak():
    # 1、可以不使用global声明直接使用全局变量
    # print(time)

    # 2、但是不能修改全局
    # time = 2
    # print(time)

    # 3、如果需要修改需要使用global
    global time
    time = 2
    print(time)


@test_decorator('nonlocal')
def talk():
    language = 'chinese'

    def inner():
        nonlocal language
        language = 'english'
        if language == 'chinese':
            print('你好！')
        elif language == 'english':
            print('hello!')

    print(language)

    inner()

    print(language)

    return


if __name__ == '__main__':
    speak()

    talk()
    talk()
